package demo1_cmp;

import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        Student student1=new Student("zhang",21);
        Student student2=new Student("yang",20);
        Student student3=new Student("wang",23);

        Student[] students=new Student[3];
        students[0]=student1;
        students[1]=student2;
        students[2]=student3;
        //做排序
        System.out.println("排序前"+ Arrays.toString(students));//默认使用的Comparable
        //Arrays.sort(students);//使用的Comparable
        //使用Comparator
        NameComparator nameComparator=new NameComparator();//这里使用了name，还可以使用age
        //Arrays.sort(students,nameComparator);

        AgeComparator ageComparator=new AgeComparator();
        Arrays.sort(students,ageComparator);//变降序可以修改AgeComparator的相减位置

        System.out.println("排序后"+ Arrays.toString(students));
        //比较大小
        if(student1.compareTo(student2)>0){
            System.out.println("student1 > student2");
        }else if(student1.compareTo(student2)<0){
            System.out.println("student1 < student2");
        }else {
            System.out.println("student1 == student2");
        }
        System.out.println("Comparator接口比较方法");
        System.out.println("======================");
        //demo1_cmp.NameComparator nameComparator=new demo1_cmp.NameComparator();
        int ret=nameComparator.compare(student1,student2);
        if(ret>0){
            System.out.println("student1 > student2");
        }else if(ret<0){
            System.out.println("student1 < student2");
        }else {
            System.out.println("student1 == student2");
        }
        //demo1_cmp.AgeComparator ageComparator=new demo1_cmp.AgeComparator();
        int ret1=ageComparator.compare(student1,student2);
        if(ret1>0){
            System.out.println("student1 > student2");
        }else if(ret1<0){
            System.out.println("student1 < student2");
        }else {
            System.out.println("student1 == student2");
        }
    }
}
